﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using WIKID.Controller;

namespace WIKID
{
    public partial class TestView : UserControl
    {
        public QuestionModel questionModel;
        //public List<CheckBox> checkBoxs;
        public int iPoint=0;

        public TestView()
        {
            InitializeComponent();
        }

        private void TestView_Load(object sender, EventArgs e)
        {
            if (!DesignMode && questionModel != null )
            {
                    
                    lblQuestionName.Text = "Question: "+questionModel.ID;
                    lblQuestionContent.Text = questionModel.Description;

                    lblQuestionName.Dock = DockStyle.Top;
                    lblQuestionContent.Dock = DockStyle.Top;

                    lblQuestionName.Font = new Font(this.Font.Name, 15, this.Font.Style);
                    lblQuestionContent.Font = new Font(this.Font.Name, 10, this.Font.Style);

                    //checkBoxs = new List<CheckBox>();

                    if (questionModel.ImagePath != "")
                    {
                        // 
                        // pictureBox1
                        // 
                        PictureBox pictureBox1 = new PictureBox();
                        int y1 = lblQuestionContent.Location.Y + lblQuestionContent.Height;
                        pictureBox1.Location = new System.Drawing.Point(22, y1);
                        pictureBox1.Name = "pictureBox1";
                        pictureBox1.TabIndex = 3;
                        pictureBox1.TabStop = false;
                        pictureBox1.Dock = DockStyle.Top;
                        try
                        {                            
                            Image image = Image.FromFile(questionModel.ImagePath);

                            pictureBox1.Image = image;
                            pictureBox1.Size = new System.Drawing.Size(image.Width, image.Height);                            
                            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                            
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Can not open the file: \n" + questionModel.ImagePath);
                            
                        }
                        this.Controls.Add(pictureBox1);
                        panel1.Location = new System.Drawing.Point(22, 218);
                    }
                    else
                    {
                        panel1.Location = new System.Drawing.Point(22, 50);
                    }
                    panel1.Dock = DockStyle.Top;
                    int x = 3;
                    int y = 10;
                    int width = 80;
                    int height = 17;
                    int tabIndex = 0;

                    foreach (AnswerModel answer in questionModel.Answers)
                    {
                        // 
                        // checkBox1
                        // 
                        CheckBox checkBox1 = new CheckBox();
                        checkBox1.AutoSize = true;
                        checkBox1.Location = new System.Drawing.Point(x, y);

                        checkBox1.Name = "CheckBox_"+ questionModel.ID+ "_" +answer.ID;
                        
                        width = answer.Content.Length + 10;
                        checkBox1.Size = new System.Drawing.Size(width, height);
                        checkBox1.TabIndex = tabIndex;
                        checkBox1.Text = answer.ID + " - " + answer.Content;
                        checkBox1.UseVisualStyleBackColor = true;
                        checkBox1.CheckedChanged += new System.EventHandler(checkBox1_CheckedChanged);
                        checkBox1.Font = new Font(this.Font.Name, 10, this.Font.Style);
                        panel1.Controls.Add(checkBox1);
                        //checkBoxs.Add(checkBox1);

                        y += height;
                        tabIndex++;
                    }
                    panel1.Size = new System.Drawing.Size(this.Width, y+10);

                }            
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox checkBox = ((CheckBox)sender);
            String[] temp = checkBox.Name.Split('_');
            String ID = temp[temp.Length - 1];
            QuestionController questionController = new QuestionController();
            if (questionController.CheckTheAnswer(questionModel.Solution, ID))
            {
                if(checkBox.Checked)
                {
                    iPoint++;
                }
            }
        }
    }
}
